﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Test2
{
    public partial class Robot : Form
    {
        URDateHandle URDateCollector = new URDateHandle();   //实例化数据类的对象
        URControlHandle URController = new URControlHandle();//实例化控制类的对象
        ModbusSocket URRegisterHandle = new ModbusSocket();  //实例化通信类的对象
        public string Target_IP;//机器人IP地址
        public int Control_Port;//控制端口
        public int DataRefreshRate;//数据刷新频率
        public double BasicSpeed;
        public double BasicAcceleration;
        
        public double SpeedRate;        //声明全局的速度
        public double AccelerationRate; //声明全局加速度控制条

        public Robot()
        {
            InitializeComponent();


            Robot.CheckForIllegalCrossThreadCalls = false;
            URDateCollector.OnGetRobotStateSuccess += new URDateHandle.GetRobotStateSuccess(UpdateRobotState);//添加机器人状态方法
            URDateCollector.OnGetPositionSuccess += new URDateHandle.GetPositionSuccess(UpdatePositionsValue);
        }

        private void Front_MouseDown(object sender, MouseEventArgs e)
        {
            string str = GetLinearMovementCommand("Y", 1);
            URController.Send_command(str);
        }
        private void Front_MouseUp(object sender, MouseEventArgs e)
        {
            string str = GetStopCommand();
            URController.Send_command(str);
        }
        private void back_MouseDown(object sender, MouseEventArgs e)
        {
            string str = GetLinearMovementCommand("Y", -1);
            URController.Send_command(str);
        }
        private void back_MouseUp(object sender, MouseEventArgs e)
        {
            string str = GetStopCommand();
            URController.Send_command(str);
        }
        private void left_MouseDown(object sender, MouseEventArgs e)
        {
            string str = GetLinearMovementCommand("X", -1);
            URController.Send_command(str);
        }
        private void left_MouseUp(object sender, MouseEventArgs e)
        {
            string str = GetStopCommand();
            URController.Send_command(str);
        }
        private void right_MouseDown(object sender, MouseEventArgs e)
        {
            string DebugDir = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory);
            // string PicDir = DebugDir + "\\Arrow\\ArrowXYZLeft_click.png";
            //this.Move_Left.Image = Image.FromFile(PicDir);

            string str = GetLinearMovementCommand("X", 1);
            URController.Send_command(str);
        }
        private void right_MouseUp(object sender, MouseEventArgs e)
        {
            string str = GetStopCommand();
            URController.Send_command(str);
        }
        private void up_MouseDown(object sender, MouseEventArgs e)
        {
            string str = GetLinearMovementCommand("Z", 1);
            URController.Send_command(str);
        }
        private void up_MouseUp(object sender, MouseEventArgs e)
        {
            string str = GetStopCommand();
            URController.Send_command(str);
        }
        private void down_MouseDown(object sender, MouseEventArgs e)
        {
            string str = GetLinearMovementCommand("Z", -1);
            URController.Send_command(str);
        }
        private void down_MouseUp(object sender, MouseEventArgs e)
        {
            string str = GetStopCommand();
            URController.Send_command(str);
        }

        void UpdateRobotState(int[] RobotState)
        {
            //只要能取到第一笔数据，就说明连接成功了
            //你也可以检测258寄存器的数据 RobotMode有很多定义，一个口的不同数字表示了多种状态
            if (RobotState[0] == 1)
            {
                string DebugDir = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory);
                string PicDir = DebugDir + "\\Button\\BtnReady.png";
                this.pictureBox2.Image = Image.FromFile(PicDir);
                this.label6.Text = "已连接";
            }

            if (RobotState[1] == 1)
            {
                string DebugDir = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory);
                string PicDir = DebugDir + "\\Button\\BtnSecurityStopped.png";
                this.pictureBox2.Image = Image.FromFile(PicDir);
                this.label6.Text = "安全停机";
            }

            if (RobotState[2] == 1)
            {
                string DebugDir = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory);
                string PicDir = DebugDir + "\\Button\\BtnEmergencyStoped.png";
                this.pictureBox2.Image = Image.FromFile(PicDir);
                this.label6.Text = "紧急停机";
            }

            if (RobotState[3] == 1)
            {
                string DebugDir = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory);
                string PicDir = DebugDir + "\\Button\\BtnTeaching.png";
                this.pictureBox2.Image = Image.FromFile(PicDir);
                this.label6.Text = "示教模式";
            }

            //最后一个判断UR是不是在运行程序就不做了，很简单，通过258寄存器取值
        }
        //将取到的数据放入文本框(当需要被通知时候触发)
        void UpdatePositionsValue(float[] Positions)
        {

            label14.Text = Positions[0].ToString("0.0");
            label15.Text = Positions[1].ToString("0.0");
            label16.Text = Positions[2].ToString("0.0");
            // U_Position.Text = Positions[3].ToString("0.000");
            // V_Position.Text = Positions[4].ToString("0.000");
            // W_Position.Text = Positions[5].ToString("0.000");
        }

        //移动命令
        string GetLinearMovementCommand(string whatAxis, int direction)
        {
            //不管怎么样都要获取当前的坐标值
            double new_X = URDateHandle.Positions_X;
            double new_Y = URDateHandle.Positions_Y;
            double new_Z = URDateHandle.Positions_Z;
            double new_U = URDateHandle.Positions_U;
            double new_V = URDateHandle.Positions_V;
            double new_W = URDateHandle.Positions_W;

            //然后根据点动的按钮，判断要改哪个值(这里不是旋转，只有X,Y,Z三种可能)，直接覆盖到真实的当前XYZ值
            if (whatAxis == "X")
            {
                new_X = ((new_X + 10) * direction);
            }
            else if (whatAxis == "Y")
            {
                new_Y = ((new_Y + 10) * direction);
            }
            else if (whatAxis == "Z")
            {
                new_Z = ((new_Z + 10) * direction);
            }
            else
            {
                //也有可能我不要移动，只是要看指令
            }
            //最后把方向运动的指令发送出去
            string command = "movel(p[" + new_X.ToString() + "," + new_Y.ToString() + "," + new_Z.ToString() + "," + new_U.ToString() + "," + new_V.ToString() + "," + new_W.ToString() + "], a = " + AccelerationRate.ToString() + ", v = " + SpeedRate.ToString() + ")";
            textBox1.Text = command;
            return command;
        }
        //停止命令
        string GetStopCommand()
        {
            string StopCommand = "stopl(1)";
            textBox1.Text = StopCommand;
            return StopCommand;
        }

        //速度
        private void Speed_Scroll(object sender, EventArgs e)
        {
            SpeedRate = BasicSpeed * Speed.Value / 20;
        }

        //加速度
        private void Acceleration_Scroll(object sender, EventArgs e)
        {
            AccelerationRate = BasicAcceleration * Acceleration.Value / 20;
        }
        

        private void button8_Click(object sender, EventArgs e)
        {
            //Target_IP = "192.168.2.3";
            Target_IP = RobotIP.Text;
            Control_Port = 30003;
            DataRefreshRate = 100;
            URDateHandle.ScanRate = DataRefreshRate;
            URDateCollector.InitialMoniter(Target_IP);
            ////建立Modbus连接
            //URRegisterHandle.RemoteIP = "10.2.72.5";
            URRegisterHandle.RemoteIP = RobotIP.Text;
            URRegisterHandle.RemotePort = 502;
            URRegisterHandle.SocketTimeOut = 1000;
            //URController.Creat_client(Target_IP, Control_Port);
            URRegisterHandle.ConnectServer();
            //URController.Creat_client(textBox1.Text, 29999);
            //初始化速度和加速度(基准速度0.15 最高变成2倍即0.2，最低变成0.1倍即0.01)
            BasicSpeed = 0.15;
            BasicAcceleration = 0.15;
            SpeedRate = BasicSpeed * Speed.Value / 10;
            AccelerationRate = BasicAcceleration * Acceleration.Value / 10;
            URController.Creat_client(Target_IP, Control_Port);

        }
    }
}
